﻿using UnityEngine;
using System.Collections;

namespace index{
	public class Main : MonoBehaviour {

		Transform pointer;
		Vector3[] opts = {
			new Vector3(9.8f, 7.3f, 0),
			new Vector3(9.8f, 5.8f, 0),
			new Vector3(9.8f, 4.3f, 0),
			new Vector3(9.8f, 2.8f, 0)
		};
		string[] optNames = {
			"play",
			"connect",
			"edit",
			"exit"
		};
		int currOptIndex = 0;

		// Use this for initialization
		void Start () {
			pointer = GameObject.FindGameObjectWithTag("POINTER").transform;
		}
		
		// Update is called once per frame
		void Update () {
			// back键 退出程序
			if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape)) {
				Application.Quit();
			}

			// 上下箭头选择
			if (Input.GetKeyDown (KeyCode.S)) {
				movePointerDown (true);
			}
			else if (Input.GetKeyDown (KeyCode.W)) {
				movePointerDown (false);
			}
			else if (Input.GetKeyDown (KeyCode.J)) {
				selectAndTo (optNames[currOptIndex]);
			}
		}

		void movePointerDown (bool down) {
			if (down) {
				++currOptIndex;
			}
			else {
				--currOptIndex;
				if (currOptIndex < 0) {
					currOptIndex += opts.Length;
				}
			}
			currOptIndex %= opts.Length;
			pointer.position = opts [currOptIndex];
		}

		public void selectAndTo (string optName) {
			if ("play".Equals (optName)) {
				currOptIndex = 0;
			}
			else if ("connect".Equals (optName)) {
				currOptIndex = 1;
			}
			else if ("edit".Equals (optName)) {
				currOptIndex = 2;
			}
			else if ("exit".Equals (optName)) {
				currOptIndex = 3;
			}

			pointer.position = opts [currOptIndex];
			StartCoroutine(loadScene (optName, 0.2f));
		}

		IEnumerator loadScene(string optName, float s) {
			yield return new WaitForSeconds (s);

			if ("exit".Equals (optName)) {
				Application.Quit ();
			}
			else if("play".Equals(optName)){
				play.Main.life = 2;
				play.PlayerState.rebornTime = Time.time;
				Application.LoadLevel (optName);

			}
			else {
				Application.LoadLevel (optName);
			}
		}
	}
}